package controller;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import biz.EmployeeBiz;
import entity.Employee;

@Controller
public class EmployeeController {
	@Resource
	private EmployeeBiz biz;
	@RequestMapping("/addEmployee")
	public String addEmployee(HttpServletRequest request) {
		//没写完，半成品
		String empname = request.getParameter("empname");
		String empage = request.getParameter("empage");
		String empsex = request.getParameter("empsex");
		String empsalary = request.getParameter("empsalary");
		String empbirthday = request.getParameter("empbirthday");
		String empdetail = request.getParameter("empdetail");
		String empdept = request.getParameter("empdept");
		
		MultipartHttpServletRequest  mrequest = (MultipartHttpServletRequest)request;			
		MultipartFile file =  mrequest.getFile("empphoto");
		String projectpath = request.getSession().getServletContext().getRealPath("/");		  
		//项目根路径
		String uploadpath = projectpath + "upload/";
		//(要提前在 webapp文件夹中建好upload文件夹)将路径连接
		String uploadfile = uploadpath + System.currentTimeMillis() + file.getOriginalFilename();	
		//file.getOriginalFilename()是文件上传上来之后本来的名字
		//在路径下写文件的名字(中间加上currentTimeMillis避免重名)(也可以不加后面的file.getOriginalFilename())
		File storefile = new File(uploadfile);
		//新建一个File，指向上传文件
		try {
			file.transferTo(storefile);
			//将文件上传
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Employee emp = new Employee();
		emp.setName(empname);
		emp.setAge(Integer.parseInt(empage));
		emp.setSex(Integer.parseInt(empsex));
		emp.setBirthday(empbirthday);
		emp.setSalary(Double.parseDouble(empsalary));
		emp.setDid(Integer.parseInt(empdept));
		emp.setDetail(empdetail);
		boolean flag = biz.addEmployee(emp);
		return "redirect:/EmployeeAdd.jsp?flag=" + flag;
	}
}
